package cn.iocoder.yudao.module.demo.controller.admin.fileinfo;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.demo.controller.admin.fileinfo.vo.DemoFileInfoPageReqVO;
import cn.iocoder.yudao.module.demo.controller.admin.fileinfo.vo.DemoFileInfoRespVO;
import cn.iocoder.yudao.module.demo.controller.admin.fileinfo.vo.DemoFileInfoSaveReqVO;
import cn.iocoder.yudao.module.demo.dal.dataobject.fileinfo.DemoFileInfoDO;
import cn.iocoder.yudao.module.demo.service.fileinfo.DemoFileInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - demo文件信息")
@RestController
@RequestMapping("/demo/file-info")
@Validated
public class DemoFileInfoController {

    @Resource
    private DemoFileInfoService fileInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建demo文件信息")
    @PreAuthorize("@ss.hasPermission('demo:file-info:create')")
    public CommonResult<Long> createFileInfo(@Valid @RequestBody DemoFileInfoSaveReqVO createReqVO) {
        return success(fileInfoService.createFileInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新demo文件信息")
    @PreAuthorize("@ss.hasPermission('demo:file-info:update')")
    public CommonResult<Boolean> updateFileInfo(@Valid @RequestBody DemoFileInfoSaveReqVO updateReqVO) {
        fileInfoService.updateFileInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除demo文件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('demo:file-info:delete')")
    public CommonResult<Boolean> deleteFileInfo(@RequestParam("id") Long id) {
        fileInfoService.deleteFileInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得demo文件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('demo:file-info:query')")
    public CommonResult<DemoFileInfoRespVO> getFileInfo(@RequestParam("id") Long id) {
        DemoFileInfoDO fileInfo = fileInfoService.getFileInfo(id);
        return success(BeanUtils.toBean(fileInfo, DemoFileInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得demo文件信息分页")
    @PreAuthorize("@ss.hasPermission('demo:file-info:query')")
    public CommonResult<PageResult<DemoFileInfoRespVO>> getFileInfoPage(@Valid DemoFileInfoPageReqVO pageReqVO) {
        PageResult<DemoFileInfoDO> pageResult = fileInfoService.getFileInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DemoFileInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出demo文件信息 Excel")
    @PreAuthorize("@ss.hasPermission('demo:file-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFileInfoExcel(@Valid DemoFileInfoPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DemoFileInfoDO> list = fileInfoService.getFileInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "demo文件信息.xls", "数据", DemoFileInfoRespVO.class,
                BeanUtils.toBean(list, DemoFileInfoRespVO.class));
    }

}
